﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace DataUpload
{
    public class EncryptBase
    {
        #region 私有属性

        /// <summary>  
        /// MD5加密的字符串  
        /// </summary>  
        private string md5Str = null;
        /// <summary>  
        /// DES加密的字符串  
        /// </summary>  
        private string encryptStr = null;

        /// <summary>  
        /// DES解密的字符串  
        /// </summary>  
        private string decryptStr = null;

        /// <summary>  
        /// DES密匙  
        /// </summary>  
        private string mydesKey = null;

        /// <summary>  
        /// 返回的字符串  
        /// </summary>  
        private string mydesStr = null;

        /// <summary>  
        /// 错误信息  
        /// </summary>  
        private string messAge = null;

        #endregion

        #region 公共属性

        /// <summary>  
        /// MD5加密字符串  
        /// </summary>  
        public string MD5Str
        {
            get { return md5Str; }
            set { md5Str = value; }
        }
        /// <summary>  
        /// DES加密的字符串  
        /// </summary>  
        public string EncryptStr
        {
            get { return encryptStr; }
            set { encryptStr = value; }
        }

        /// <summary>  
        ///DES 解密的字符串  
        /// </summary>  
        public string DecryptStr
        {
            get { return decryptStr; }
            set { decryptStr = value; }
        }

        /// <summary>  
        /// DES密匙  
        /// </summary>  
        public string MyDesKey
        {
            get { return mydesKey; }
            set { mydesKey = value; }
        }

        /// <summary>  
        /// 返回的字符串  
        /// </summary>  
        public string MyDesStr
        {
            get { return mydesStr; }
            set { mydesStr = value; }
        }
        /// <summary>  
        /// 错误信息  
        /// </summary>  
        public string Message
        {
            get { return messAge; }
            set { messAge = value; }
        }
        #endregion

        #region 公共方法
        /// <summary>  
        /// 执行DES加密  
        /// </summary>  
        public void DesEncrypt()
        {
            try
            {
                byte[] MyStr_E = Encoding.UTF8.GetBytes(this.encryptStr);
                byte[] MyKey_E = Encoding.UTF8.GetBytes(this.mydesKey);

                DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
                MyDes_E.Key = MyKey_E;
                MyDes_E.IV = MyKey_E;

                MemoryStream MyMem_E = new MemoryStream();

                CryptoStream MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
                MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
                MyCry_E.FlushFinalBlock();
                MyCry_E.Close();

                this.mydesStr = Convert.ToBase64String(MyMem_E.ToArray());
            }
            catch (Exception Error)
            {
                this.messAge = "DES加密出错：" + Error.Message;
            }
        }

        /// <summary>  
        /// 执行DES解密  
        /// </summary>  
        public void DesDecrypt()
        {
            try
            {
                byte[] MyStr_D = Convert.FromBase64String(this.decryptStr);
                byte[] MyKey_D = Encoding.UTF8.GetBytes(this.mydesKey);

                DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
                MyDes_D.Key = MyKey_D;
                MyDes_D.IV = MyKey_D;

                MemoryStream MyMem_D = new MemoryStream();

                CryptoStream MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);
                MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
                MyCry_D.FlushFinalBlock();
                MyCry_D.Close();

                this.mydesStr = Encoding.UTF8.GetString(MyMem_D.ToArray());
            }
            catch (Exception Error)
            {
                this.messAge = "DES解密出错：" + Error.Message;
            }
        }

        /// <summary>  
        /// 执行MD5加密  
        /// </summary>  
        public void MD5JiaMi()
        {
            MD5CryptoServiceProvider MyMD5 = new MD5CryptoServiceProvider();
            try
            {
                Byte[] MyMD5_Str = MyMD5.ComputeHash(Encoding.UTF8.GetBytes(this.md5Str));
                this.MyDesStr = Encoding.UTF8.GetString(MyMD5_Str);
            }
            catch (Exception Error)
            {
                this.messAge = "MD5加密出错：" + Error.Message;
            }
        }

        #endregion  

        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        
        /// <summary>
        ///  DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源串</returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
    }
}
